package com.zetyun.tiger.format.rate;

import com.zetyun.rt.meta.annotation.ActionMeta;
import com.zetyun.rt.sdk.action.MapAction;
import com.zetyun.rt.sdk.model.RtEvent;
import com.zetyun.rt.sdk.operator.OperatorContext;

@ActionMeta(
        id = "tiger.format-rate",
        tags = {"formatter"},
        category = "map",
        name = "FormatRateAction",
        parameterClass = FormatRateParameter.class,
        description = "格式化汇率信息"
)
public class FormatRateAction extends MapAction {

    private FormatRateParameter param;

    @Override
    public void init(OperatorContext context) {
        super.init(context);
        param = context.getParameters();

    }

    @Override
    public RtEvent apply(RtEvent in) throws Exception {

        String theKey = in.getField("CMCRT_KEY", String.class).trim();
        if (theKey.length() != 14) {
            in.setValue(param.getCurrKeyField(), "");
            in.setValue(param.getEffDtField(), "");
            in.setValue(param.getOutField(), "");
            in.setValue(param.getInField(), "");
            in.setValue(param.getFxrTypField(), "");
            return in;
        }
        String currKey = theKey.substring(8, 14);
        String date = theKey.substring(0, 8);
        String outCode = theKey.substring(8, 10);
        String inCode = theKey.substring(10, 12);
        String type = theKey.substring(12, 14);

        in.setValue(param.getCurrKeyField(), currKey);
        in.setValue(param.getEffDtField(), date);
        in.setValue(param.getOutField(), outCode);
        in.setValue(param.getInField(), inCode);
        in.setValue(param.getFxrTypField(), type);

        return in;
    }
}
